package org.example.operation;
import java.util.*;
import org.example.model.*;
import org.example.inout.*;
import org.example.trans.*;

/**
 * @author lenovo
 */

public class UserOperation {
    //加载HashMap
    public static void loadUser(){
        if(Save.getHashMapUser() == null){
            Save.setHashMapUser(TransUser.transUserRead(InOut.read("saveUser.txt")));
        }
        if(Save.getHashMapUser() == null){
            Save.setHashMapUser(new HashMap<>());
        }
    }
    //检测username,password是否不符合登录条件
    public static boolean isNotLogin(String username, String password){
        loadUser();
        HashMap<String,User> hashMap = Save.getHashMapUser();
        return !hashMap.containsKey(username) ||
                !hashMap.get(username).getPassword().equals(password);
    }
    //检测username是否不符合注册条件
    public static boolean isNotResister(String username){
        loadUser();
        HashMap<String,User> hashMap = Save.getHashMapUser();
        return hashMap.containsKey(username);
    }
    //将username,password添加到HashMap中，并写入文件
    public static void addUser(String username,String password){
        loadUser();
        HashMap<String,User> hashMap = Save.getHashMapUser();
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        hashMap.put(username,user);
        Save.setHashMapUser(hashMap);
        InOut.write(TransUser.transUserWrite(Save.getHashMapUser()),"saveUser.txt");
    }
    //删除HashMap中的username，并写入文件
    public static void deleteUser(String username){
        loadUser();
        HashMap<String,User> hashMap = Save.getHashMapUser();
        hashMap.remove(username);
        Save.setHashMapUser(hashMap);
        InOut.write(TransUser.transUserWrite(Save.getHashMapUser()),"saveUser.txt");
    }
}
